package com.kxmall.web.controller.rider.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.rider.domain.KxRiderOrder;
import com.kxmall.rider.domain.bo.KxRiderOrderBo;
import com.kxmall.rider.domain.vo.KxRiderOrderVo;
import com.kxmall.rider.mapper.KxRiderOrderMapper;
import com.kxmall.web.controller.rider.service.IKxRiderOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 配送员订单信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-09-21
 */
@RequiredArgsConstructor
@Service
public class KxRiderOrderServiceImpl implements IKxRiderOrderService {

    private final KxRiderOrderMapper baseMapper;

    /**
     * 查询配送员订单信息
     */
    @Override
    public KxRiderOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询配送员订单信息列表
     */
    @Override
    public TableDataInfo<KxRiderOrderVo> queryPageList(KxRiderOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxRiderOrder> lqw = buildQueryWrapper(bo);
        Page<KxRiderOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询配送员订单信息列表
     */
    @Override
    public List<KxRiderOrderVo> queryList(KxRiderOrderBo bo) {
        LambdaQueryWrapper<KxRiderOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxRiderOrder> buildQueryWrapper(KxRiderOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KxRiderOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStatus() != null, KxRiderOrder::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), KxRiderOrder::getReason, bo.getReason());
        lqw.eq(bo.getRiderId() != null, KxRiderOrder::getRiderId, bo.getRiderId());
        lqw.eq(bo.getUserId() != null, KxRiderOrder::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getConsignee()), KxRiderOrder::getConsignee, bo.getConsignee());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), KxRiderOrder::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), KxRiderOrder::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), KxRiderOrder::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getCounty()), KxRiderOrder::getCounty, bo.getCounty());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), KxRiderOrder::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getMono()), KxRiderOrder::getMono, bo.getMono());
        lqw.eq(bo.getStorageId() != null, KxRiderOrder::getStorageId, bo.getStorageId());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), KxRiderOrder::getStoreName, bo.getStoreName());
        lqw.eq(bo.getOrderId() != null, KxRiderOrder::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), KxRiderOrder::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getTotalPrice() != null, KxRiderOrder::getTotalPrice, bo.getTotalPrice());
        lqw.eq(bo.getTotalPreferentialPrice() != null, KxRiderOrder::getTotalPreferentialPrice, bo.getTotalPreferentialPrice());
        lqw.eq(bo.getFreightPrice() != null, KxRiderOrder::getFreightPrice, bo.getFreightPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getPayChannel()), KxRiderOrder::getPayChannel, bo.getPayChannel());
        lqw.eq(bo.getPayTime() != null, KxRiderOrder::getPayTime, bo.getPayTime());
        lqw.eq(bo.getOrderCreateTime() != null, KxRiderOrder::getOrderCreateTime, bo.getOrderCreateTime());
        lqw.eq(bo.getPredictTime() != null, KxRiderOrder::getPredictTime, bo.getPredictTime());
        lqw.eq(bo.getAbnormal() != null, KxRiderOrder::getAbnormal, bo.getAbnormal());
        lqw.eq(bo.getLongitude() != null, KxRiderOrder::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatitude() != null, KxRiderOrder::getLatitude, bo.getLatitude());
        lqw.eq(bo.getFinishTime() != null, KxRiderOrder::getFinishTime, bo.getFinishTime());
        return lqw;
    }

    /**
     * 新增配送员订单信息
     */
    @Override
    public Boolean insertByBo(KxRiderOrderBo bo) {
        KxRiderOrder add = BeanUtil.toBean(bo, KxRiderOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改配送员订单信息
     */
    @Override
    public Boolean updateByBo(KxRiderOrderBo bo) {
        KxRiderOrder update = BeanUtil.toBean(bo, KxRiderOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxRiderOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除配送员订单信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
